/*
 *******************************************************************************
 *                                EIT Car Project
 *                             Main FUNCTIONS
 *
 *                     (c) Copyright 2015-2020 Car@EIT 
 *                            All Rights Reserved
 * File      : EIT_Motor.c
 * This file is part of EIT Car Project
 * Embedded Innovation Team(EIT) - Car@EIT  
 * ----------------------------------------------------------------------------
 * LICENSING TERMS:
 * 
 *     CarLib is provided in source form for FREE evaluation and  educational 
 * use.
 *    If you plan on using  EITLib  in a commercial product you need to contact 
 * Car@EIT to properly license its use in your product. 
 * 
 * ----------------------------------------------------------------------------
 * Change Logs:
 * Date           Author       Notes
 * 2015-01-22     Xian.Chen    the first version
 *
 *******************************************************************************
 */

#include "EIT_Motor.h"

void Motor1_init()
{
   gpio_init (MOTOR1_EN, GPO,0);
   ftm_pwm_init(MOTOR1_FTM, MOTOR1_PWMA, MOTOR1_PWM_FREQ,0);
   ftm_pwm_init(MOTOR1_FTM, MOTOR1_PWMB, MOTOR1_PWM_FREQ,0);
}
void Motor1_Start(int32 pwm)
{
   uint32 PWM_A =0;
   uint32 PWM_B =0;
   
   
   if( pwm >MOTOR1_PWM_MAX )
   {
      PWM_A = MOTOR1_PWM_MAX;
   }
   else if(pwm < MOTOR1_PWM_MIN)
   {
      PWM_B = MOTOR1_PWM_MAX;
   }
   else if ( pwm >0 )
   {
      PWM_A = pwm;
   }
   else if ( pwm <0 )
   {
      PWM_B = -pwm;
   }
      
   ftm_pwm_duty(MOTOR1_FTM, MOTOR1_PWMA,PWM_A);
   ftm_pwm_duty(MOTOR1_FTM, MOTOR1_PWMB,PWM_B);
   gpio_init ( MOTOR1_EN, GPO,1);
}
void Motor1_Brake(void)
{   
   ftm_pwm_duty(MOTOR1_FTM, MOTOR1_PWMA,0);
   ftm_pwm_duty(MOTOR1_FTM, MOTOR1_PWMB,0);
   gpio_init ( MOTOR1_EN, GPO,1);
}
void Motor1_Slip(void)
{
   gpio_init (MOTOR1_EN, GPO,0);
   ftm_pwm_duty(MOTOR1_FTM, MOTOR1_PWMA,0);
   ftm_pwm_duty(MOTOR1_FTM, MOTOR1_PWMB,0);
}

void Motor2_init()
{
   gpio_init (MOTOR2_EN, GPO,0);
   ftm_pwm_init(MOTOR2_FTM, MOTOR2_PWMA, MOTOR2_PWM_FREQ,0);
   ftm_pwm_init(MOTOR2_FTM, MOTOR2_PWMB, MOTOR2_PWM_FREQ,0);
}
void Motor2_Start(int32 pwm)
{
   uint32 PWM_A =0;
   uint32 PWM_B =0;
   
   
   if( pwm >MOTOR2_PWM_MAX )
   {
      PWM_A = MOTOR2_PWM_MAX;
   }
   else if(pwm < MOTOR2_PWM_MIN)
   {
      PWM_B = MOTOR2_PWM_MAX;
   }
   else if ( pwm >0 )
   {
      PWM_A = pwm;
   }
   else if ( pwm <0 )
   {
      PWM_B = -pwm;
   }
      
   ftm_pwm_duty(MOTOR2_FTM, MOTOR2_PWMA,PWM_A);
   ftm_pwm_duty(MOTOR2_FTM, MOTOR2_PWMB,PWM_B);
   gpio_init ( MOTOR2_EN, GPO,1);
}
void Motor2_Brake(void)
{   
   ftm_pwm_duty(MOTOR2_FTM, MOTOR2_PWMA,0);
   ftm_pwm_duty(MOTOR2_FTM, MOTOR2_PWMB,0);
   gpio_init ( MOTOR2_EN, GPO,1);
}
void Motor2_Slip(void)
{
   gpio_init (MOTOR2_EN, GPO,0);
   ftm_pwm_duty(MOTOR2_FTM, MOTOR2_PWMA,0);
   ftm_pwm_duty(MOTOR2_FTM, MOTOR2_PWMB,0);
}